package com.yanqu.road.server.manger.maidian.mansionroom;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MansionRoomLog {

    public static final String MANSION_ROOM = "mansion_room";
    public static final String MANSION_LOOK = "mansion_look";
    public static final String MANSION_SUMADD = "mansion_sumadd";

    public static final String MANSION_NAME = "mansion_name";
    public static final String MANSION_LV = "mansion_lv";
    public static final String MANSION_ADD = "mansion_add";


    public static void uploadSnapshot(GamePlayer player) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction action = new DefaultEventTraceAction(MANSION_ROOM, player);

            int addition = 0;
            List<Map<String, Object>> objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, UserSkin> skinEntry : player.getModule(SkinModule.class).getUserSkinMap().entrySet()) {
                UserSkin userSkin = skinEntry.getValue();
                if(SkinMgr.isEstateSkin(userSkin.getSkinId())){
                    Map<String, Object> map = new HashMap<>();
                    map.put(MANSION_NAME, userSkin.getSkinId());
                    map.put(MANSION_LV, userSkin.getLevel());

                    SkinInfo skinInfo = SkinMgr.getSkinInfo(userSkin.getSkinId());
                    if(skinInfo != null){
                        int add = 0;
                        for (int skillId : skinInfo.getSkinEffect()) {
                            SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                            if(skillInfo != null){
                                add += SkillMgr.getSkillAddition(skillInfo, userSkin.getLevel());
                            }
                        }
                        map.put(MANSION_ADD, add);
                        addition += add;
                    }
                    objectGroup.add(map);
                }
            }
            if(objectGroup.size() == 0){
                return;
            }
            action.build(MANSION_LOOK, objectGroup);
            action.build(MANSION_SUMADD, addition);

            action.trace();
        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }
}
